/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pisa;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import javax.swing.JFrame;
import yarp.Bottle;

/**
 *
 * @author samf
 */
public class PisaApp {

    JFrame frame;
    YarpListen listen;
    PisaRobot robot;
    private boolean shouldRun;

    public static void main(String args[]) {
        PisaApp app = new PisaApp();
        app.start();
    }

    void start() {
       robot = new PisaRobot();
        PisaUtils utils = new PisaUtils();
        HashMap<String, String> configs = utils.readConfig();
        
        //robot.playAnimation("wave");
        String os = configs.get("os");
       
        //      foods = Utils.readSet("resources/foods.txt");
        listen = new YarpListen(this, os);
        listen.start();
        frame = new JFrame();
        frame.setSize(400, 300);
        frame.setTitle("R25-Control");
        // frame.setLocation(600, 600);
        frame.setVisible(true);
        shouldRun = true;
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                shouldRun = false;
            }
        });

        while (shouldRun) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        listen.close();
        frame.dispose();
        System.exit(0);
    }

    void doAction(Bottle bot) {
        String action = bot.get(0).asString();
        System.out.println(action);
        if (action.equals("wave")) {
            robot.playAnimation("wave");
        }
        if (action.equals("smile")) {
            robot.playAnimation("happy");
        }
        if (action.equals("sad")) {
            robot.playAnimation("sad");
        }

        if (action.startsWith("speak")) {
            String speech = bot.get(1).asString();
            System.out.println(speech);
            robot.speak(speech);
        }
    }
}
